package org.example.platform.beans.factory;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * JDK 实例化
 */
public class SimpleInstantiationStrategy implements InstantiationStrategy{

    @Override
    public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) {
       Class clazz = beanDefinition.getBean();
       try{
           if(null != clazz){
               return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args);
           }else {
               return clazz.getDeclaredConstructor().newInstance();
           }
       } catch (InvocationTargetException |InstantiationException |IllegalAccessException |NoSuchMethodException e) {
           throw new RuntimeException(clazz.getName()+e);
       }
    }
}
